package com.metadata_system.common.db.entity;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.persistence.*;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.metadata_system.system.response.NodePropertiesItem;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.DigestUtils;

@Data
@Table(name = "md_project_node")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ProjectNode {
    @Id
    private Integer id;

    /**
     * 项目ID
     */
    @Column(name = "project_id")
    private Integer projectId;

    /**
     * 元数据-ID
     */
    @Column(name = "metadata_id")
    private Integer metadataId;

    /**
     * 元数据-节点-ID
     */
    @Column(name = "metadata_node_id")
    private Integer metadataNodeId;

    /**
     * 元数据-节点-名称
     */
    @Column(name = "metadata_node_name")
    private String metadataNodeName;

    /**
     * 元数据-节点-属性
     */
    @Column(name = "metadata_node_properties")
    private String metadataNodeProperties;

    /**
     * 元数据-节点-属性-唯一标记
     */
    @Column(name = "metadata_node_properties_key")
    private String metadataNodePropertiesKey;

    /**
     * 数据总条数
     */
    @Column(name = "data_total")
    private Integer dataTotal;

    /**
     * 创建时间
     */
    @Column(name = "created_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date updatedTime;


    /**
     * 生成唯一键
     *
     * @param properties 节点属性
     * @param name       节点名称
     * @return
     */
    public static String getPropertiesKey(String properties, String name) {
        if (properties == null) {
            return "";
        }
        List<NodePropertiesItem> nodePropertiesItems = JSON.parseArray(properties, NodePropertiesItem.class);
        List<String> propertiesKeys = new ArrayList<>();
        nodePropertiesItems.forEach(item -> {
            propertiesKeys.add(item.getLabel());
        });
        return getPropertiesKeyByNames(propertiesKeys, name);
    }


    /**
     * 生成唯一键
     *
     * @param propertiesNames 属性名称列表
     * @param name            节点名称
     * @return
     */
    public static String getPropertiesKeyByNames(List<String> propertiesNames, String name) {

        List<String> nameArr = new ArrayList<>();
        for (String propertiesName : propertiesNames) {
            if (!propertiesName.equalsIgnoreCase("ID")) {
                nameArr.add(propertiesName);
            }
        }

        nameArr.sort(Comparator.naturalOrder());


        String str = JSON.toJSONString(nameArr);
        System.out.println("str: " + str);
        System.out.println("name: " + name);
        String md5Str = DigestUtils.md5DigestAsHex((str + name).getBytes());
        System.out.println("md5Str: " + md5Str);
        return md5Str;
    }
}